package com.jn.algorithm.day010.question31;

import java.util.Arrays;

/**
 * @author 江南大学1033190417
 * @date 2022/4/25 23:16
 */
public class Solution {

    public void nextPermutation(int[] nums) {
        int length = nums.length;
        for (int i = length - 2; i >= 0; i--) {
            if (i == 0 && nums[i] >= nums[i + 1]) {
                Arrays.sort(nums);
                return;
            }
            if (nums[i] < nums[i + 1]) {//找到开始递增的地方
                for (int j = length - 1; j > i; j--) {
                    if (nums[j] > nums[i]) {
                        //ij交换位置
                        int temp = nums[i];
                        nums[i] = nums[j];
                        nums[j] = temp;
                        Arrays.sort(nums, i + 1, length);
                        break;
                    }
                }
                break;
            }
        }
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        int[] nums = new int[]{3, 2, 1,0};
        System.out.println(Arrays.toString(nums));
        Arrays.sort(nums,1,4);
        System.out.println(Arrays.toString(nums));


    }
}
